﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessLayer.Tables;

public partial class CrmList : ChildPage
{
    string targetTextBox = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.Count > 0)
        {
            ViewState["StokPage"] = Request.QueryString["stok"];
            ViewState["ischeckbox"] = Request.QueryString["check"];
            ViewState["listType"] = Request.QueryString["ltype"];
            ViewState["controlID"] = Request.QueryString["ctrl"];
            ViewState["VerilecekUrun"] = Request.QueryString["VUrun"];
            ViewState["HProStok"] = Request.QueryString["HProStok"];
            ViewState["HProCariAdres"] = Request.QueryString["HProCariAdres"];
            ViewState["HProKdv"] = Request.QueryString["HProKdv"];
            targetTextBox = Request["ctrl"];
            if (ViewState["listType"] != null)
                GridViewButtonKontrol("listType");
            else if (ViewState["HProStok"] != null)
                GridViewCheckBoxKontrol();
            else if (ViewState["HProCariAdres"] != null)
                GridViewButtonKontrol("listType");
            else if (ViewState["HProKdv"] != null)
                GridViewButtonKontrol("listType");
            else if (ViewState["VerilecekUrun"] != null)
                GridViewCheckBoxKontrol();
            if (Convert.ToBoolean(ViewState["StokPage"]) == true)
            {
                GridViewCheckBoxKontrol();
                this.btnOk.Attributes.Add("onclick", "ReKeyStok(document.getElementById('" + HiddenField1.ClientID + "'));");
            }
            this.btnCancel.Attributes.Add("onclick", "CancelButton();");
        }
    }

    #region Fields
    Db_Stok stok;
    Db_Musteri musteri;
    DataTable dtStok;
    DataTable dtMusteri;
    Db_Reyon reyon;
    DataTable dtReyon;
    #endregion

    #region Function Tablolar
    private void StokTable()
    {
        stok = new Db_Stok();
        dtStok = stok.GetAllRecord_Datatable("stok_kodu as [Stok Kodu]", "stok_adi as [Stok Adı]", "stok_adi2 as [Stok Adı2]");
        grdList.DataSource = dtStok;
        grdList.DataBind();
    }
    private void CariTable()
    {
        musteri = new Db_Musteri();
        dtMusteri = musteri.GetAllRecord_Datatable2("SELECT musteri_kod AS [Müşteri Kodu], musteri_adi AS [Müşteri Adı] FROM TBL_0_MUSTERI");
        grdList.DataSource = dtMusteri;
        grdList.DataBind();
    }
    private void HProCari(string search)
    {
        musteri = new Db_Musteri();
        if (search == "null")
            dtMusteri = musteri.GetAllRecord_Datatable2("SELECT musteri_kod, musteri_adi, ev_mahalle FROM TBL_0_MUSTERI");
        else
            dtMusteri = musteri.GetAllRecord_Datatable2("SELECT musteri_kod, musteri_adi, ev_mahalle FROM TBL_0_MUSTERI WHERE (ev_mahalle = N'" + search + "')");
        grdList.DataSource = dtMusteri;
        grdList.DataBind();
    }
    private void HProKdv()
    {
        stok = new Db_Stok();
        dtStok = stok.GetAllRecord_Datatable("stok_kodu as [Stok Kodu]", "stok_adi as [Stok Adı]", "stok_adi2 as [Stok Adı2]");
        grdList.DataSource = dtStok;
        grdList.DataBind();
    }
    private void PrmReyonTbl()
    {
        reyon = new Db_Reyon();
        dtReyon = reyon.GetAllRecord_Datatable2("SELECT ryn_reyon_kod AS [Reyon Kodu], ryn_reyon_ad AS [Reyon Adı]FROM TBL_0_REYON");
        grdList.DataSource = dtReyon;
        grdList.DataBind();
    }
    #endregion

    #region ListTypeRun
    private void ListTypeRunCari()
    {
        string s = string.Empty;
        for (int i = 0; i < grdList.Rows.Count; i++)
        {
            GridViewRow row = grdList.Rows[i];
            bool chek = ((CheckBox)row.FindControl("CheckBox1")).Checked;
            if (chek == true)
            {
                s += grdList.Rows[i].Cells[2].Text;
                s += ";";
            }
        }
        HiddenField1.Value = s;
        this.ClientScript.RegisterStartupScript(typeof(Page), "Kapat", "ReKey('" + targetTextBox + "',document.getElementById('" + HiddenField1.ClientID + "'));", true);
    }
    private void ListTypeRun()
    {
        string s = string.Empty;
        for (int i = 0; i < grdList.Rows.Count; i++)
        {
            GridViewRow row = grdList.Rows[i];
            bool chek = ((CheckBox)row.FindControl("CheckBox1")).Checked;
            if (chek == true)
            {
                s += grdList.Rows[i].Cells[3].Text;
                s += ";";
            }
        }
        HiddenField1.Value = s;
        this.ClientScript.RegisterStartupScript(typeof(Page), "Kapat", "ReKey('" + targetTextBox + "',document.getElementById('" + HiddenField1.ClientID + "'));", true);
    }
    private void ListTypeRun2()
    {
        string s = string.Empty;
        for (int i = 0; i < grdList.Rows.Count; i++)
        {
            GridViewRow row = grdList.Rows[i];
            bool chek = ((CheckBox)row.FindControl("CheckBox1")).Checked;
            if (chek == true)
            {
                s += grdList.Rows[i].Cells[2].Text;
                s += ";";
            }
        }
        HiddenField1.Value = s;
        this.ClientScript.RegisterStartupScript(typeof(Page), "Kapat", "ReKey('" + targetTextBox + "',document.getElementById('" + HiddenField1.ClientID + "'));", true);
    }
    private void HizliPromStok()
    {
        this.ClientScript.RegisterStartupScript(typeof(Page), "Kapat", "ReKey('" + targetTextBox + "',document.getElementById('" + HiddenField1.ClientID + "'));", true);
    }

    #endregion

    protected void btnGo_OnClick(object sender, EventArgs e)
    {
        if (Convert.ToBoolean(ViewState["StokPage"]) == true)
            StokTable();
        else if (Convert.ToBoolean(ViewState["HProStok"]) == true)
            StokTable();
        else if (Convert.ToBoolean(ViewState["HProCariAdres"]) == true)
        {
            if (txtSearch.Text == "")
                HProCari("null");
            else
                HProCari(txtSearch.Text);
        }
        else if (Convert.ToBoolean(ViewState["HProKdv"]) == true)
            HProKdv();
        else if (ViewState["VerilecekUrun"] != null)
        {
            if (ViewState["VerilecekUrun"].ToString() == "3")
                StokTable();
        }
        else if (ViewState["listType"] != null)
        {
            switch (ViewState["listType"].ToString())
            {
                case "0":
                    CariTable();
                    break;
                case "1":
                    StokTable();
                    break;
                case "2":
                    PrmReyonTbl();
                    break;
                default:
                    break;
            }
        }

    }
    public bool GridControl()
    {
        bool sonuc = false;
        if (Convert.ToBoolean(ViewState["ischeckbox"]) == true)
        {
            sonuc = true;
        }
        return sonuc;
    }
    private void GridViewButtonKontrol(string type)
    {
        switch (type)
        {
            case "listType":
                grdList.Columns[0].Visible = false;
                break;

        }
    }
    private void GridViewCheckBoxKontrol()
    {
        grdList.Columns[1].Visible = false;
    }
    protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Convert.ToBoolean(ViewState["HProStok"]) == true)
        {
            if (grdList.Rows[Convert.ToInt32(e.CommandArgument)].Cells[3].Text.ToString() != "")
                HiddenField1.Value = grdList.Rows[Convert.ToInt32(e.CommandArgument)].Cells[3].Text.ToString();
        }
        else if (grdList.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text.ToString() != "")
            HiddenField1.Value = grdList.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text.ToString();
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (ViewState["listType"] != null)
        {
            if (ViewState["listType"].ToString() == "0")
                ListTypeRunCari();
            else
                ListTypeRun();
        }
        if (ViewState["HProStok"] != null)
            HizliPromStok();
        if (ViewState["HProCariAdres"] != null)
            ListTypeRun();
        if (ViewState["HProKdv"] != null)
            ListTypeRun2();
        if (ViewState["VerilecekUrun"] != null)
            HizliPromStok();
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {

    }
}
